package com.net.tkc.pro.controller;

import com.net.tkc.pro.entity.Picture;
import com.net.tkc.pro.entity.ResultEntity;
import com.net.tkc.pro.entity.XiaoClass;
import com.net.tkc.pro.impl.PictureImpl;
import com.net.tkc.pro.tool.JsonUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/Img/")
public class PictureController {
    ResultEntity resultEntity = null;
    @Resource
    PictureImpl pictureImpl;

    /**
     * 添加轮播图
     * @param picture 图片地址
     * @param explain 描述信息
     * @return
     */
    @PostMapping("Img")
    private String addPicture(
            @RequestParam("picture") String picture,
            @RequestParam("explain") String explain
    ){
        resultEntity = new ResultEntity();
        Picture picture1=new Picture();
        picture1.setPicture(picture);
        picture1.setExplain(explain);
        try{
            int result =pictureImpl.addPicture(picture1);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("添加失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    /**
     * 更新轮播图信息
     * @param picture
     * @param explain
     * @param examine
     * @param id
     * @return
     */
    @PutMapping("Img")
    private String upPicture(
            @RequestParam("picture") String picture,
            @RequestParam("explain") String explain,
            @RequestParam("examine") int examine,
            @RequestParam("id") int id
    ){
        resultEntity = new ResultEntity();
        try{
            int result = pictureImpl.upPicture(picture, explain, examine, id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("修改成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("修改失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    /**
     * 无条件查询轮播里面的数据
     * @return
     */
    @GetMapping("Img")
    private String getList(
    ){
        resultEntity = new ResultEntity();
        try{
            List<Picture> list =pictureImpl.getList();
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
}
